<?php
/**
 * 分销
 *
 */



defined('In33hao') or exit('Access Invalid!');

class fenxiaoControl extends mobileMemberControl {

    public function __construct(){
        parent::__construct();
    }

    public function member_listOp() {
        $model_member = Model('member');
        $level = $_POST['lev'] ? $_POST['lev'] : 1;
        $where['lev'.$level] = $this->member_info['member_id'];
        $member_list = $model_member->getFenxiaoMembersList($where, 10,'member_id desc', 'member_id,member_name,fenxiao_num');
        $page_count = $model_member->gettotalpage();
        output_data(array('member_list' => $member_list), mobile_page($page_count));
    }

    /**
     * 会员实物订单列表
     */
    public function order_listOp() {
        $model_member = Model('member');
        $where['lev1'] = $this->member_info['member_id'];
        $where['lev2'] = $this->member_info['member_id'];
        $where['lev3'] = $this->member_info['member_id'];
        $where['_op'] = 'or';
        $member_ids = $model_member->getMemberList($where,'member_id');
        if (empty($member_ids)) {
            output_data(array('order_group_list' => []), mobile_page(0));
        }
        $ids = '';
        foreach($member_ids as $v) {
            $ids[] = $v['member_id'];
        }
        $model_order = Model('order');
        $condition = array();
        $condition['buyer_id'] = ['in',implode(',',$ids)];
        $condition['order_type'] = array('in',array(1,3));

        if ($_POST['state_type'] != '') {
            $condition['order_state'] = str_replace(
                array('state_new','state_send','state_noeval'),
                array(ORDER_STATE_NEW,ORDER_STATE_SEND,ORDER_STATE_SUCCESS), $_POST['state_type']);
        }
        if ($_POST['state_type'] == 'state_new') {
            $condition['chain_code'] = 0;
        }
        if ($_POST['state_type'] == 'state_noeval') {
            $condition['evaluation_state'] = 0;
            $condition['order_state'] = ORDER_STATE_SUCCESS;
        }
        if ($_POST['state_type'] == 'state_notakes') {
            $condition['order_state'] = array('in',array(ORDER_STATE_NEW,ORDER_STATE_PAY));
            $condition['chain_code'] = array('gt',0);
        }

        if (preg_match('/^\d{10,20}$/',$_POST['order_key'])) {
            $condition['order_sn'] = $_POST['order_key'];
        } elseif ($_POST['order_key'] != '') {
            $condition['order_id'] = array('in',$this->_getOrderIdByKeyword($_POST['order_key']));
        }

        $order_list_array = $model_order->getNormalOrderList($condition, $this->page, '*', 'order_id desc','', array('order_goods'));
        $model_refund_return = Model('refund_return');
        $order_list_array = $model_refund_return->getGoodsRefundList($order_list_array,1);//订单商品的退款退货显示

        $ownShopIds = Model('store')->getOwnShopIds();

        $order_group_list = array();
        $order_pay_sn_array = array();
        foreach ($order_list_array as $value) {
            $value['zengpin_list'] = array();
            //显示取消订单
            $value['if_cancel'] = $model_order->getOrderOperateState('buyer_cancel',$value);
            //显示退款取消订单
            $value['if_refund_cancel'] = $model_order->getOrderOperateState('refund_cancel',$value);
            //显示收货
            $value['if_receive'] = $model_order->getOrderOperateState('receive',$value);
            //显示锁定中
            $value['if_lock'] = $model_order->getOrderOperateState('lock',$value);
            //显示物流跟踪
            $value['if_deliver'] = $model_order->getOrderOperateState('deliver',$value);
            //显示评价
            $value['if_evaluation'] = $model_order->getOrderOperateState('evaluation',$value);
            //显示追加评价
            $value['if_evaluation_again'] = $model_order->getOrderOperateState('evaluation_again',$value);
            //显示删除订单(放入回收站)
            $value['if_delete'] = $model_order->getOrderOperateState('delete',$value);

            $value['ownshop'] = in_array($value['store_id'], $ownShopIds);

            //商品图
            foreach ($value['extend_order_goods'] as $k => $goods_info) {
                $value['extend_order_goods'][$k]['goods_image_url'] = cthumb($goods_info['goods_image'], 240, $value['store_id']);
                $value['extend_order_goods'][$k]['refund'] = $value['extend_order_goods'][$k]['refund'] ? true : false;
                unset($value['extend_order_goods'][$k]['rec_id']);
                unset($value['extend_order_goods'][$k]['order_id']);
                unset($value['extend_order_goods'][$k]['goods_pay_price']);
                unset($value['extend_order_goods'][$k]['store_id']);
                unset($value['extend_order_goods'][$k]['buyer_id']);
                unset($value['extend_order_goods'][$k]['promotions_id']);
                unset($value['extend_order_goods'][$k]['commis_rate']);
                unset($value['extend_order_goods'][$k]['gc_id']);
                unset($value['extend_order_goods'][$k]['goods_contractid']);
                unset($value['extend_order_goods'][$k]['goods_image']);
                if ($value['extend_order_goods'][$k]['goods_type'] == 5) {
                    $value['zengpin_list'][] = $value['extend_order_goods'][$k];
                    unset($value['extend_order_goods'][$k]);
                }
            }
            if ($value['extend_order_goods']) {
                $value['extend_order_goods'] = array_values($value['extend_order_goods']);
            }

            $order_group_list[$value['pay_sn']]['order_list'][] = $value;

            //如果有在线支付且未付款的订单则显示合并付款链接
            if ($value['order_state'] == ORDER_STATE_NEW) {
                $order_group_list[$value['pay_sn']]['pay_amount'] += $value['order_amount'] - $value['rcb_amount'] - $value['pd_amount'];
            }
            $order_group_list[$value['pay_sn']]['add_time'] = $value['add_time'];

            //记录一下pay_sn，后面需要查询支付单表
            $order_pay_sn_array[] = $value['pay_sn'];
        }
        $new_order_group_list = array();
        foreach ($order_group_list as $key => $value) {
            $value['pay_sn'] = strval($key);
            $new_order_group_list[] = $value;
        }

        $page_count = $model_order->gettotalpage();

        output_data(array('order_group_list' => $new_order_group_list), mobile_page($page_count));
    }

    private function _getOrderIdByKeyword($keyword) {
        $goods_list = Model('order')->getOrderGoodsList(array('goods_name'=>array('like','%'.$keyword.'%')),'order_id',100,null,'', null,'order_id');
        return array_keys($goods_list);
    }

    /**
     * 会员虚拟订单列表
     */
    public function vr_order_listOp() {

        $ownShopIds = Model('store')->getOwnShopIds();
        $model_vr_order = Model('vr_order');
        $model_member = Model('member');

        $where['lev1'] = $this->member_info['member_id'];
        $where['lev2'] = $this->member_info['member_id'];
        $where['lev3'] = $this->member_info['member_id'];
        $where['_op'] = 'or';
        $member_ids = $model_member->getMemberList($where,'member_id');
        if (empty($member_ids)) {
            output_data(array('order_list' => []), mobile_page(0));
        }
        $ids = '';
        foreach($member_ids as $v) {
            $ids[] = $v['member_id'];
        }
        $condition = array();
        $condition['buyer_id'] = ['in',implode(',',$ids)];
        if (preg_match('/^\d{10,20}$/',$_POST['order_key'])) {
            $condition['order_sn'] = $_POST['order_key'];
        } elseif ($_POST['order_key'] != '') {
            $condition['goods_name'] = array('like','%'.$_POST['order_key'].'%');
        }
        if ($_POST['state_type'] != '') {
            $condition['order_state'] = str_replace(
                array('state_new','state_pay'),
                array(ORDER_STATE_NEW,ORDER_STATE_PAY), $_POST['state_type']);
        }
        $order_list = $model_vr_order->getOrderList($condition, $this->page, '*', 'order_id desc');

        foreach ($order_list as $key => $order) {
            //显示取消订单
            $order_list[$key]['if_cancel'] = $model_vr_order->getOrderOperateState('buyer_cancel',$order);

            //显示支付
            $order_list[$key]['if_pay'] = $model_vr_order->getOrderOperateState('payment',$order);

            //显示评价
            $order_list[$key]['if_evaluation'] = $model_vr_order->getOrderOperateState('evaluation',$order);

            $order_list[$key]['goods_image_url'] = cthumb($order['goods_image'], 240, $order['store_id']);

            $order_list[$key]['ownshop'] = in_array($order['store_id'], $ownShopIds);
        }

        $page_count = $model_vr_order->gettotalpage();

        output_data(array('order_list' => $order_list), mobile_page($page_count));
    }


    /**
     * 会员推广明细列表
     */
    public function log_listOp() {
        $model_brokerage_log = Model('brokerage_log');
        $res = $model_brokerage_log->getHalfYearMemberLogList($this->member_info['member_id'],3,$_GET['curpage']);
        output_data(array('log_list' => $res['data']), ['hasmore'=>$res['hasmore']]);
    }




    /**
     * 小店实物订单列表
     */
    public function store_order_listOp() {
        $model_order = Model('order');
        $condition['my_store_id'] = $_POST['store_id'];
        if ($_POST['state_type'] == 'state_new') {
            $condition['order_status'] = 10;
        }
        if ($_POST['state_type'] == 'state_send') {
            $condition['order_status'] = ['in',[ORDER_STATE_PAY,ORDER_STATE_SEND]];
        }
        if ($_POST['state_type'] == 'state_noeval') {
            $condition['order_status'] = ORDER_STATE_SUCCESS;
        }
        if (preg_match('/^\d{10,20}$/',$_POST['order_key'])) {
            $condition['order_sn'] = $_POST['order_key'];
        } elseif ($_POST['order_key'] != '') {
            $condition['order_id'] = array('in',$this->_getOrderIdByKeyword($_POST['order_key']));
        }
        $fields = 'goods_name,goods_image,goods_num,goods_pay_price,goods_price,goods_spec,store_rates,store_id,order_status';
        $order_group_list = $model_order->getOrderGoodsList($condition,$fields,$this->page);
        if (empty($order_group_list)) {
            output_data(array('order_group_list' => []), mobile_page(0));
        }
        foreach ($order_group_list as $k => $v) {
            $order_group_list[$k]['goods_image_url'] = cthumb($v['goods_image'], 240, $v['store_id']);
            unset($order_group_list[$k]['goods_image']);
        }
        $page_count = $model_order->gettotalpage();

        output_data(array('order_group_list' => $order_group_list), mobile_page($page_count));
    }

    /**
     * 小店虚拟订单列表
     */
    public function store_vr_order_listOp() {

        $model_vr_order = Model('vr_order');
        $condition['my_store_id'] = $_POST['store_id'];
        if ($_POST['state_type'] == 'state_new') {
            $condition['order_state'] = 10;
        }
        if ($_POST['state_type'] == 'state_send') {
            $condition['order_state'] = ['in',[ORDER_STATE_PAY,ORDER_STATE_SEND]];
        }
        if ($_POST['state_type'] == 'state_noeval') {
            $condition['order_state'] = ORDER_STATE_SUCCESS;
        }
        if (preg_match('/^\d{10,20}$/',$_POST['order_key'])) {
            $condition['order_sn'] = $_POST['order_key'];
        } elseif ($_POST['order_key'] != '') {
            $condition['order_id'] = array('in',$this->_getOrderIdByKeyword($_POST['order_key']));
        }
        $fields = 'goods_name,goods_image,goods_num,order_amount,goods_price,goods_spec,store_rates,store_id,order_state';
        $order_list = $model_vr_order->getOrderList($condition,$this->page,$fields,'order_id desc',10);
        if (empty($order_list)) {
            output_data(array('order_list' => []), mobile_page(0));
        }
        foreach ($order_list as $k => $v) {
            $order_list[$k]['goods_image_url'] = cthumb($v['goods_image'], 240, $v['store_id']);
            unset($order_list[$k]['goods_image']);
        }

        $page_count = $model_vr_order->gettotalpage();

        output_data(array('order_list' => $order_list), mobile_page($page_count));
    }


    /**
     * 小店佣金明细列表
     */
    public function store_log_listOp() {
        $model_brokerage_log = Model('brokerage_log');
        $res = $model_brokerage_log->getHalfYearStoreLogList($_POST['store_id'],3,$_GET['curpage']);
        output_data(array('log_list' => $res['data']), ['hasmore'=>$res['hasmore']]);
    }
}
